/*
 * $Id: ClientPendingChangelistsContainer.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.ui.model;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.api.IChangesResult;
import net.sourceforge.p4eclipse.core.api.IInfoResult;
import net.sourceforge.p4eclipse.core.api.IOpenedResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * This class works as a container for pending changelists of this client.
  *
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ClientPendingChangelistsContainer extends AbstractPendingChangelistsContainer
{
	private static final IChangesResult DEFAULT_CHANGES_RESULT = new DefaultChangesResult();
	
	/** @see IWorkbenchAdapter#getLabel(Object) */
	public String getLabel(Object o) 
	{ 
		if (!(o instanceof ClientPendingChangelistsContainer))
			return null;
			
		return MessageFormat.format("Pending Changelists (client ''{0}'')", new Object[]{ PerforceUIPlugin.getClientName() }); 
	}

	/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
	public ImageDescriptor getImageDescriptor(Object o)
	{
		if (!(o instanceof ClientPendingChangelistsContainer))
			return null;
		
		return PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_CLIENT_CHANGELISTS);
	}
	
	/** @see IWorkbenchAdapter#getChildren(Object) */
	public Object[] getChildren(Object o)
	{
		if (!(o instanceof ClientPendingChangelistsContainer))
			return null;
			
		List result = new ArrayList();
		result.add(DEFAULT_CHANGES_RESULT);
		
		try
		{
			IInfoResult infoResult = PerforceUIPlugin.getClient().info();
			IChangesResult[] changesResults = PerforceUIPlugin.getClient().changes("pending", 100);
			for (int i = 0, length = changesResults.length; i < length; i++)
			{
				if (changesResults[i].getClientName().equals(infoResult.getClientName()))
					result.add(changesResults[i]);	
			}
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.getPlugin().log(e.getStatus());
		}
		
		return result.toArray();
	}

	/**
	  * This class represents an implementation of IChangesResult for the
	  * default changelist.
	  */
	private static class DefaultChangesResult implements IChangesResult, IWorkbenchAdapter
	{
		/** @see IChangesResult#getChangelist() */
		public Integer getChangelist() { return new Integer(-1); }

		/** @see IChangesResult#getClientName() */
		public String getClientName() 
		{ 
			return PerforceUIPlugin.getClient().info().getClientName();
		}

		/** @see IChangesResult#getDate() */
		public Date getDate() { return new Date(); }

		/** @see IChangesResult#getDescription() */
		public String getDescription() { return ""; }

		/** @see IChangesResult#getStatus() */
		public String getStatus() { return "pending"; }

		/** @see IChangesResult#getUserName() */
		public String getUserName()
		{
			return PerforceUIPlugin.getClient().info().getUserName();
		}
		
		/** @see IWorkbenchAdapter#getChildren(Object) */
		public Object[] getChildren(Object o)
		{ 
			if (!(o instanceof DefaultChangesResult))
				return null; 
								
			IOpenedResult[] result = new IOpenedResult[0];
			try
			{
				IChangesResult changesResult = (IChangesResult)o;
				result = PerforceUIPlugin.getClient().opened(getChangelist());
			}
			catch (PerforceException e)
			{
				PerforceUIPlugin.getPlugin().log(e.getStatus());
			}
			
			return result;
		}

		/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
		public ImageDescriptor getImageDescriptor(Object o) 
		{ 
			if (!(o instanceof DefaultChangesResult))
				return null; 
			
			return PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_CLIENT_CHANGELIST);
		}

		/** @see IWorkbenchAdapter#getLabel(Object) */
		public String getLabel(Object o) { return "Default"; }

		/** @see IWorkbenchAdapter#getParent(Object) */
		public Object getParent(Object o) { return null; }

		/** @see IAdaptable#getAdapter(Class) */
		public Object getAdapter(Class adapter) 
		{ 
			return (adapter == IWorkbenchAdapter.class) ? this : null; 
		}	
	}
}
